package com.ruoyi.project.modules.virtualUsageParamSource.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.domain.ParamsFieldRelation;
import com.ruoyi.project.modules.parameter.mapper.InterfaceParamsFieldRelationMapper;
import com.ruoyi.project.modules.parameter.mapper.ParamsFieldRelationMapper;
import com.ruoyi.project.modules.virtualUsageParamSource.vo.ObjectParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.virtualUsageParamSource.mapper.VirtualUsageParamSourceMapper;
import com.ruoyi.project.modules.virtualUsageParamSource.domain.VirtualUsageParamSource;
import com.ruoyi.project.modules.virtualUsageParamSource.service.IVirtualUsageParamSourceService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 虚拟用况入参来源Service业务层处理
 *
 * @author smallrain
 * @date 2022-03-31
 */
@Service
public class VirtualUsageParamSourceServiceImpl implements IVirtualUsageParamSourceService {
    @Resource
    private VirtualUsageParamSourceMapper virtualUsageParamSourceMapper;

    /**
     * 查询虚拟用况入参来源
     *
     * @param virtualUsageParamSourceId 虚拟用况入参来源主键
     * @return 虚拟用况入参来源
     */
    @Override
    public VirtualUsageParamSource selectVirtualUsageParamSourceByVirtualUsageParamSourceId(Long virtualUsageParamSourceId) {
        return virtualUsageParamSourceMapper.selectVirtualUsageParamSourceByVirtualUsageParamSourceId(virtualUsageParamSourceId);
    }

    /**
     * 查询虚拟用况入参来源列表
     *
     * @param virtualUsageParamSource 虚拟用况入参来源
     * @return 虚拟用况入参来源
     */
    @Override
    public List<VirtualUsageParamSource> selectVirtualUsageParamSourceList(VirtualUsageParamSource virtualUsageParamSource) {
        return virtualUsageParamSourceMapper.selectVirtualUsageParamSourceList(virtualUsageParamSource);
    }

    /**
     * 新增虚拟用况入参来源
     *
     * @param virtualUsageParamSource 虚拟用况入参来源
     * @return 结果
     */
    @Override
    public int insertVirtualUsageParamSource(VirtualUsageParamSource virtualUsageParamSource) {

        return virtualUsageParamSourceMapper.insertVirtualUsageParamSource(virtualUsageParamSource);
    }

    private void redundancy(VirtualUsageParamSource virtualUsageParamSource) {
        if (virtualUsageParamSource.getType().equals("1")) {
            InterfaceParamsFieldRelation interfaceParamsFieldRelation = this.interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationById(virtualUsageParamSource.getObjectId());
            InterfaceParamsFieldRelation interfaceParamsFieldRelationSource = this.interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationById(virtualUsageParamSource.getSourceobjectId());
            virtualUsageParamSource.setSourceInterfaceId(interfaceParamsFieldRelationSource.getInterfaceId());
            virtualUsageParamSource.setSourceFieldId(interfaceParamsFieldRelationSource.getObjectId());
            if (interfaceParamsFieldRelationSource.getId().equals(interfaceParamsFieldRelation.getId())) {
                virtualUsageParamSource.setSourceInterfaceType("3");
            } else {

            }
        }
    }

    /**
     * 修改虚拟用况入参来源
     *
     * @param virtualUsageParamSource 虚拟用况入参来源
     * @return 结果
     */
    @Override
    public int updateVirtualUsageParamSource(VirtualUsageParamSource virtualUsageParamSource) {
        return virtualUsageParamSourceMapper.updateVirtualUsageParamSource(virtualUsageParamSource);
    }

    /**
     * 批量删除虚拟用况入参来源
     *
     * @param virtualUsageParamSourceIds 需要删除的虚拟用况入参来源主键
     * @return 结果
     */
    @Override
    public int deleteVirtualUsageParamSourceByVirtualUsageParamSourceIds(String virtualUsageParamSourceIds) {
        return virtualUsageParamSourceMapper.deleteVirtualUsageParamSourceByVirtualUsageParamSourceIds(Convert.toStrArray(virtualUsageParamSourceIds));
    }

    /**
     * 删除虚拟用况入参来源信息
     *
     * @param virtualUsageParamSourceId 虚拟用况入参来源主键
     * @return 结果
     */
    @Override
    public int deleteVirtualUsageParamSourceByVirtualUsageParamSourceId(Long virtualUsageParamSourceId) {
        return virtualUsageParamSourceMapper.deleteVirtualUsageParamSourceByVirtualUsageParamSourceId(virtualUsageParamSourceId);
    }

    @Resource
    private InterfaceParamsFieldRelationMapper interfaceParamsFieldRelationMapper;

    @Resource
    private ParamsFieldRelationMapper paramsFieldRelationMapper;

    @Override
    public List<ObjectParam> objectParamList(Long interfaceId, String interfaceType, String isInParam, String objectType, Long sourceId) {
        if (objectType.equals("3")) {
            return this.paramsFieldRelationMapper.objectParamList(sourceId);
        }
        return this.interfaceParamsFieldRelationMapper.objectParamList(interfaceId, interfaceType, isInParam, objectType);
    }

    @Override
    public List<VirtualUsageParamSource> selectVirtualUsageParamSourceList2(VirtualUsageParamSource virtualUsageParamSource) {
        return virtualUsageParamSourceMapper.selectVirtualUsageParamSourceList2(virtualUsageParamSource);
    }

    @Override
    public void saveOutParamFieldSource(VirtualUsageParamSource virtualUsageParamSource) {
        this.virtualUsageParamSourceMapper.deleteByObjectIdAndType(virtualUsageParamSource.getObjectId(), virtualUsageParamSource.getType());
        if (CollectionUtil.isNotEmpty(virtualUsageParamSource.getFieldId())) {
            List<VirtualUsageParamSource> virtualUsageParamSourceList = new ArrayList<>();
            for (String fieldId : virtualUsageParamSource.getFieldId()) {
                String[] s = fieldId.split("_");
                VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
                paramSource.setInterfaceId(virtualUsageParamSource.getInterfaceId());
                paramSource.setObjectId(virtualUsageParamSource.getObjectId());
                paramSource.setType(virtualUsageParamSource.getType());
                paramSource.setSourceobjectId(Long.parseLong(s[0]));
                paramSource.setSourceType(s[1]);
                paramSource.setIsInParam("2");
                virtualUsageParamSourceList.add(paramSource);
            }
            this.virtualUsageParamSourceMapper.batchSave(virtualUsageParamSourceList);
        }
    }
}
